<?php /* Smarty version 2.6.26, created on 2010-05-18 21:19:53
         compiled from cms.library.3.dialog.tpl */ ?>
<h1>
    Bibliotek
</h1>
<br/>
<div>
    <div id="libIndexHeader"><span>Bibliotek</span><span id="libraryPath">library:<span>#PATH NOT SET#</span></span></div>
    <div id="libIndexWrapper">
        <div id="libraryContent">
            <div class="libIndexContainerWrapper"><div class="libIndexContainerHeader"><span>Billeder</span></div>
                <div class="libIndexContainer" style="height:auto;">
                    <div id="imagelist">

                    </div>
                    <div class="clear"></div>
                </div>

            </div>
            <div class="libIndexContainerWrapper"><div class="libIndexContainerHeader"><span>Mapper</span> &nbsp;|&nbsp; <span id="addFolder">Ny mappe</span></div>
                <div class="libIndexContainer"  style="height:auto;">
                    <div id="folderlist">


                    </div>
                    <div class="clear"></div>
                </div>
            </div>
        </div>
        <div id="libraryLoadingAnimation"></div>
    </div>
    <div class="clear"></div>
</div>


<script type="text/javascript">
    var liburl = "<?php echo $this->_tpl_vars['LIBRARYURL']; ?>
";
    var jsonUrl = "<?php echo $this->_tpl_vars['libraryCallUrl']; ?>
";

    //and now the literal  <?php echo '

    var DEFAULT_THUMBNAIL_HEIGHT = 120;
    var DEFAULT_THUMBNAIL_WIDTH =160;

    var jsonData = {};
    var jsonCustomCallback = function(){};
    var folderName = "";
    var lastMouseMove = 0;


    function time(){
        var t = new Date();
        return Math.round(t.getTime());
    }


    $(function(){ // on dom ready
        $(\'#addFolder\').click(addFolder);
        $(window).mousemove(function(){
            lastMouseMove = time();
        });

        refresh(); // refresh library
        lastMouseMove = time();
        autoRefresh();
    });

    function autoRefresh(){
        if (time() - lastMouseMove > 5000){
            refresh();
            lastMouseMove = time();
        }
        setTimeout(autoRefresh, 1000);
    }



    function libraryLoadingAnimation(b){
        if(b){
            $("#libraryContent").hide();
            $("#libraryLoadingAnimation").show();
        }else{
            $("#libraryContent").show();
            $("#libraryLoadingAnimation").hide();
        }
    }

    function refresh(){
        busy(true);
        refreshLibraryData(function(data){
            jsonData = data;
            updateIndex(function(){
                attachImageEvents();
                attachFolderEvents();
                libraryLoadingAnimation(false);
                busy(false);
            });
        });
    }


    function attachImageEvents(){
        $(\'a[rel*=insertbox]\').click(function(e){
            e.preventDefault();
            insertImage($(this).attr("href"));
        });

        $("#imagelist .item").draggable({
            revert:"invalid",
            handle:".header",
            start:function(e,ui){
                $(this).fadeTo(200,0.5);
                $(this).addClass("drag_on_top");
            },
            stop:function(e,ui){
                $(this).fadeTo(150,1);
                $(this).removeClass("drag_on_top");
            }
        });
    }

    function attachFolderEvents(){
        $("#folderlist .item .thumbnail").click(function(){
            folderName = $(this).parent().attr(\'rel\');
            $(\'#upload_button\').uploadifySettings("folder",folderName);
            $(\'#imagelist\').empty();
            $(\'#folderlist\').empty();
            libraryLoadingAnimation(true);
            refresh();
        });

        $("#folderlist .item").droppable({
            accept:".item",
            hoverClass:"folderDropHover",
            drop:function(e,ui){;
                moveFile(ui.draggable,$(this));
            }
        });

        $("#folderlist .moveable").draggable({
            revert:"invalid",
            handle:".header",
            zIndex:100,
            opacity:0.5
        });
    }


    function moveFile(eFile,eFolder){
        eFile.fadeTo(400,0.05);
        $.get("?q=command:do;action:library.file.move;ajax;file:"+eFile.attr(\'rel\')+";to:"+eFolder.attr(\'rel\')+";",function(data){
            if(data == "OK"){
                eFile.hide();
                $.jGrowl(\'<p></p>Billedet blev flyttet\', {
                    theme: 	\'success\',
                    header: \'Flyttet\',
                    life:	1000,
                    sticky: false
                });
            }
            else{
                $.jGrowl(\'<p></p>Kunne ikke flytte billedet - \' +data, {
                    theme: 	\'error\',
                    header: \'Fejl!\',
                    life:	1000,
                    sticky: true
                });
            }
            refresh();
        });
    }


    function createThumbnailUrl(url,height,width){
        if(!height)
            height = DEFAULT_THUMBNAIL_HEIGHT;
        if(!width)
            width = DEFAULT_THUMBNAIL_WIDTH;
        return liburl+"/image.php?height="+height+"&width="+width+"&image="+url;
    }
    function createImageUrl(url){
        return liburl+"/image.php?image="+url;
    }

    function updateIndex(callback){

        if(folderName != "")
            $("#libraryPath span").html(folderName);
        else
            $("#libraryPath span").html("/");

        // add images
        $(\'#imagelist\').empty();
        var fragment = document.createDocumentFragment(); // create fragment to work much faster with the dom
        for(var i in jsonData.images){
            fragment.appendChild(createItem("image", jsonData.images[i], jsonData.images[i]));
        }
        document.getElementById("imagelist").appendChild(fragment); //append fragment to correct position

        // add folders
        $(\'#folderlist\').empty();
        var fragment = document.createDocumentFragment(); // create fragment to work much faster with the dom

        //create the back folder
        var fCheck = folderName.split("/");
        if(fCheck.length > 1){ // if you CAN go back
            fCheck = fCheck.slice(0,fCheck.length-1);
            var backUrl = fCheck.join("/");
            if(backUrl == "")
                desc = "/";
            fragment.appendChild(createItem("folder",backUrl,desc,true));
        }

        for(var i in jsonData.folders){
            fragment.appendChild(createItem("folder", jsonData.folders[i],jsonData.folders[i]));
        }
        document.getElementById("folderlist").appendChild(fragment); //append fragment to correct position

        if(callback)
            callback();
    }

    function deleteImage($imageItem){
        if(confirm("Slet dette billede?")){
            $.get("?q=command:do;action:library.image.remove;ajax;image:"+$imageItem.attr("rel"),function(data){
                if(data == "OK"){
                    $imageItem.hide(300,function(){
                        refresh();
                    });
                }
                else{
                    $.jGrowl(\'<p></p>Kunne ikke slette billedet\', {
                        theme: 	\'error\',
                        header: \'Fejl!\',
                        life:	1000,
                        sticky: false
                    });
                }
            });
        }
    }

    function deleteFolder($folderItem){
        if(confirm("Er du sikker på at du vil slette denne mappe?\\nAlle billeder og undermapper vil også blive slettet!")){
            $.get("?q=command:do;action:library.folder.remove;ajax;folder:"+$folderItem.attr(\'rel\'),function(data){
                if(data == "OK"){
                    $folderItem.hide(300,function(){
                        refresh();
                    });
                }
                else{
                    $.jGrowl(\'<p></p>Kunne ikke slette mappen\', {
                        theme: 	\'error\',
                        header: \'Fejl!\',
                        life:	1000,
                        sticky: false
                    });
                }
            });
        }
    }

    function addFolder(){
        var name = prompt("Navn på ny mappe:");
        if(name){
            $.get("?q=command:do;action:library.folder.add;ajax;folder:"+folderName+"/"+name,function(data){
                if(data == "OK")
                    refresh();
                else{
                    $.jGrowl(\'<p></p>Kunne ikke oprette mappen\', {
                        theme: 	\'error\',
                        header: \'Fejl!\',
                        life:	1000,
                        sticky: false
                    });
                }
            });
        }
    }

    function createItem(type,url,desc,secure){ // Create item
        var descTxt = desc;
        var item = document.createElement("div");
        item.setAttribute("class","item");
        item.setAttribute("rel", url);

        var thumbnail = document.createElement("a");
        thumbnail.setAttribute("class","thumbnail");

        var description = document.createElement("div");
        description.setAttribute("class","description");

        var header = document.createElement("div");
        header.setAttribute("class","header");
        header.appendChild(document.createTextNode(desc));


        item.appendChild(header);
        item.appendChild(thumbnail);
        item.appendChild(description);

        if(!secure){ // if not secured item, make deleteable
            var deleteBtn = document.createElement("span");
            deleteBtn.setAttribute("class", "deleteBtn");
            deleteBtn.setAttribute("rel",url);
            deleteBtn.appendChild(document.createTextNode("Slet"));

            description.appendChild(deleteBtn);

            $(item).addClass("moveable");
        }

        switch (type){
            case "image":
                thumbnail.setAttribute("href", createImageUrl(url));
                thumbnail.setAttribute("rel", "insertbox");
                thumbnail.setAttribute("style", "background:#333 url(\'"+createThumbnailUrl(url)+"\') no-repeat center center;");

                if(!secure){
                    $(deleteBtn).click(function(e){
                        deleteImage($(this).parent().parent());
                        return false;
                    });
                }
                break;
            case "folder":
                item.setAttribute("rel", url);
                var thumbText = url.split("/");
                thumbText = thumbText[thumbText.length-1];
                var descText = thumbText;
                if(secure){
                    var backTextLink = url;
                    if(url == "")
                        backTextLink = "/";
                    descText = "Tilbage til "+backTextLink;
                    thumbText = "Tilbage";


                    $(description).css({color:"#999"});
                    $(thumbnail).css({\'background\':"#333 url(\'style/folders/Generic.png\') no-repeat center center"});
                }else{
                    $(thumbnail).css({\'background\':"#333 url(\'style/folders/Generic.png\') no-repeat center center"});
                }
                thumbnail.appendChild(document.createTextNode(thumbText));
                if(!secure){
                    $(deleteBtn).click(function(e){
                        deleteFolder($(this).parent().parent());
                        return false;
                    });
                }
                descTxt = descText;
                break;
        }
        description.appendChild(document.createTextNode(descTxt));
        return item;
    }

    function refreshLibraryData(callback){
        $.getJSON(jsonUrl+folderName,function(data){
            callback(data);
        });
    }



    function busy(b){
        if(b){
            $(\'#busy_animation .s_ready\').fadeOut(500);
        }else{
            $(\'#busy_animation .s_ready\').fadeIn(500);
        }
    }
    //end of the literal  '; ?>

</script>